var forumID;
var pollID;
var pollShouldRefresh;
var pollSelection;
var pollRefreshInterval = 30; // poll refresh seconds
//TODO: Change to dynamic interval
var pollDetails;

function initializePoll()
{
	var async = new Async("GetPoll", getPollCompleted);
	
	pollDetails = document.getElementById("PollDetails");
	
	async.Execute(forumID);
}

function getPollCompleted(results)
{
	var resultArray = results.split(",");
	
	pollID = Number(resultArray[0]);
	pollShouldRefresh = getBoolean(resultArray[1]);
	
	if (pollShouldRefresh)
		setTimeout(refreshPoll, pollRefreshInterval * 1000);
}

function refreshPoll()
{
	var async = new Async("GetPollResults", getPollResultsCompleted);
	
	async.Execute(pollID);
	
	setTimeout(refreshPoll, pollRefreshInterval * 1000);
}

function getPollResultsCompleted(results)
{
	pollDetails.innerHTML = results;
}

function SetPollSelection(selection)
{
	pollSelection.value = selection;
}

function Vote()
{
	var async = new Async("Vote", getPollResultsCompleted)
	
	pollShouldRefresh = true;
	
	async.Execute(pollID, pollSelection);
}

function getBoolean(value)
{
	if (value == "false" || value == "0" || value == "" || value.toLowerCase() == "false")
		return false;
	else if (value == "true" || value == "1" || value == "-1" || value.toLowerCase() == "true")
		return true;
	else
		return false;
}